﻿using System;
using System.Windows;
using System.Windows.Input;
using Nomad.WebSync.Extensions;

namespace Nomad.WebSync.Windows {
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class mainWindow : Window {
        /// <summary>
        /// Creates a instance of mainWindow class
        /// </summary>
        public mainWindow() {
            InitializeComponent();
        }

        private void initializeApplication() {
            App.WebSyncHeader = null;
            mainFrame.Navigate(new Pages.Login());
        }
        private void menuControl() {
            var currentFrame = mainFrame.NavigationService.Content.GetType().Name;
            var online = (App.WebSyncHeader != null);

            startMenuIcon.IsHitTestVisible = online;
            startMenuLabel.IsHitTestVisible = online;
            closeButton.Visibility = (online && (currentFrame != "Today") ? Visibility.Visible : Visibility.Hidden);

            mainNewMenuItem.Visibility = Visibility.Collapsed;
            mainSystemMenuItem.Visibility = Visibility.Collapsed;
            contactsToolsMenuItem.Visibility = Visibility.Collapsed;
            switch (currentFrame) {
                case "ContactDetail":
                    contactsToolsMenuItem.Visibility = Visibility.Visible;
                    break;
                case "Contacts":
                    mainNewMenuItem.Visibility = Visibility.Visible;                    
                    break;
                case "Login":                    
                    mainSystemMenuItem.Visibility = Visibility.Visible;                    
                    break;
                case "Today":
                    mainNewMenuItem.Visibility = Visibility.Visible;
                    break;
                default:
                    mainSystemMenuItem.Visibility = (online ? Visibility.Collapsed : Visibility.Visible);
                    mainNewMenuItem.Visibility = (online ? Visibility.Visible : Visibility.Collapsed);
                    break;
            }
        }

        private void setShutdownArea(MouseEventArgs e, bool execute) {
            var shutdownArea = e.GetPosition(this);
            if ((shutdownArea.Y > 12 && shutdownArea.Y < 48) && (shutdownArea.X > 30 && shutdownArea.X < 70)) {
                if (execute)
                    App.Current.Shutdown();
                else
                    Cursor = Cursors.Hand;
            } else {
                Cursor = Cursors.Arrow;
            }
        }
        private void moveWindow(MouseButtonEventArgs e) {
            var dragArea = e.GetPosition(windowBorder);
            if ((dragArea.X < 0 || dragArea.Y < 0) || (dragArea.X > windowBorder.Width || dragArea.Y > windowBorder.Height))
                DragMove();
        }

        private void openStartMenu() {
            startMenuIcon.ContextMenu.PlacementRectangle = new Rect() {
                Location = new Point(this.Left + 38, this.Top + 90)
            };
            startMenuIcon.ContextMenu.IsOpen = !startMenuIcon.ContextMenu.IsOpen;
        }
        private void openNewAccountForm() {
            mainFrame.Navigate(new Pages.NewAccount());
        }
        private void openSettingsForm() {
            mainFrame.Navigate(new Pages.Settings());
        }
        private void openContactsForm() {
            mainFrame.Navigate(new Pages.Contacts());
        }

        private void Window_Initialized(object sender, EventArgs e) {
            this.Cursor = Cursors.Wait;
            try {
                this.initializeApplication();
            } catch (Exception ex) {
                MessageBox.Show(ex.SoapMessage(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            } finally {
                this.Cursor = Cursors.Arrow;
            }
        }
        private void Window_MouseMove(object sender, MouseEventArgs e) {
            try {
                this.setShutdownArea(e, false);
            } catch (Exception ex) {
                MessageBox.Show(ex.SoapMessage(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
            try {
                this.setShutdownArea(e, true);
                this.moveWindow(e);
            } catch (Exception ex) {
                MessageBox.Show(ex.SoapMessage(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void mainFrame_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e) {
            this.Cursor = Cursors.Wait;
            try {
                this.menuControl();
            } catch (Exception ex) {
                MessageBox.Show(ex.SoapMessage(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            } finally {
                this.Cursor = Cursors.Arrow;
            }
        }
        private void startMenuIcon_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
            try {
                this.openStartMenu();
            } catch (Exception ex) {
                MessageBox.Show(ex.SoapMessage(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        private void closeButton_Click(object sender, RoutedEventArgs e) {
            this.Cursor = Cursors.Wait;
            try {
                if (mainFrame.NavigationService.CanGoBack)
                    mainFrame.NavigationService.GoBack();
            } catch (Exception ex) {
                MessageBox.Show(ex.SoapMessage(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            } finally {
                this.Cursor = Cursors.Arrow;
            }
        }
        private void systemNewAccountMenuItem_Click(object sender, RoutedEventArgs e) {
            this.Cursor = Cursors.Wait;
            try {
                this.openNewAccountForm();
            } catch (Exception ex) {
                MessageBox.Show(ex.SoapMessage(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            } finally {
                this.Cursor = Cursors.Arrow;
            }
        }
        private void systemSettingsMenuItem_Click(object sender, RoutedEventArgs e) {
            this.Cursor = Cursors.Wait;
            try {
                this.openSettingsForm();
            } catch (Exception ex) {
                MessageBox.Show(ex.SoapMessage(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            } finally {
                this.Cursor = Cursors.Arrow;
            }
        }
        private void startContactsMenuItem_Click(object sender, RoutedEventArgs e) {
            this.Cursor = Cursors.Wait;
            try {
                this.openContactsForm();
            } catch (Exception ex) {
                MessageBox.Show(ex.SoapMessage(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            } finally {
                this.Cursor = Cursors.Arrow;
            }
        }

        private void startCalendarMenuItem_Click(object sender, RoutedEventArgs e) {
            this.Cursor = Cursors.Wait;
            try {
                throw new Exception("Not Implemeted");
            } catch (Exception ex) {
                MessageBox.Show(ex.SoapMessage(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            } finally {
                this.Cursor = Cursors.Arrow;
            }
        }
        private void startNotesMenuItem_Click(object sender, RoutedEventArgs e) {
            this.Cursor = Cursors.Wait;
            try {
                throw new Exception("Not Implemeted");
            } catch (Exception ex) {
                MessageBox.Show(ex.SoapMessage(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            } finally {
                this.Cursor = Cursors.Arrow;
            }
        }
        private void startTasksMenuItem_Click(object sender, RoutedEventArgs e) {
            this.Cursor = Cursors.Wait;
            try {
                throw new Exception("Not Implemeted");
            } catch (Exception ex) {
                MessageBox.Show(ex.SoapMessage(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            } finally {
                this.Cursor = Cursors.Arrow;
            }
        }
        private void newAppointmentMenuItem_Click(object sender, RoutedEventArgs e) {
            this.Cursor = Cursors.Wait;
            try {
                throw new Exception("Not Implemeted");
            } catch (Exception ex) {
                MessageBox.Show(ex.SoapMessage(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            } finally {
                this.Cursor = Cursors.Arrow;
            }
        }
        private void newContactMenuItem_Click(object sender, RoutedEventArgs e) {
            this.Cursor = Cursors.Wait;
            try {
                throw new Exception("Not Implemeted");
            } catch (Exception ex) {
                MessageBox.Show(ex.SoapMessage(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            } finally {
                this.Cursor = Cursors.Arrow;
            }
        }
        private void newNoteMenuItem_Click(object sender, RoutedEventArgs e) {
            this.Cursor = Cursors.Wait;
            try {
                throw new Exception("Not Implemeted");
            } catch (Exception ex) {
                MessageBox.Show(ex.SoapMessage(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            } finally {
                this.Cursor = Cursors.Arrow;
            }
        }
        private void newTaskMenuItem_Click(object sender, RoutedEventArgs e) {
            this.Cursor = Cursors.Wait;
            try {
                throw new Exception("Not Implemeted");
            } catch (Exception ex) {
                MessageBox.Show(ex.SoapMessage(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            } finally {
                this.Cursor = Cursors.Arrow;
            }
        }
    }
}
